package com.example.controller;

import com.example.consts.RbmConsts;
import com.example.pojo.dto.SearchDTO;
import com.example.pojo.dto.SearchWordDTO;
import com.example.pojo.entity.Post;
import com.example.service.PostService;
import com.example.service.SearchService;
import com.example.vo.Result;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.ibatis.annotations.Insert;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 本文来演示:招聘端
 *
 * @author 赵建云
 * @create 2023/9/29
 */
@RestController
@RequestMapping("employment")
public class EmploymentController{
    @Autowired
    private PostService postService;
    @Autowired
    private SearchService searchService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("{jrId}")
    public Result getByJrID(@PathVariable Integer jrId) {
        List<Post> postList = postService.findAllPost(jrId);
        return new Result(200, "该招聘者发布的全部职位", postList);
    }

    @GetMapping("page")
    public Result getByPage(Integer jrId,Integer page, Integer limit) {
        PageInfo<Post> pageInfo = postService.findAllByPage(jrId, page, limit);
        List<Post> postList = pageInfo.getList();
        return new Result(0, "分页成功", Integer.valueOf(String.valueOf(pageInfo.getTotal())), postList);
    }

    @PostMapping("save")
    public Result save(Post post){
        System.out.println(post);
        int count = postService.save(post);
        if (count!=0){
            rabbitTemplate.convertAndSend(RbmConsts.MESSAGE_EX,RbmConsts.MESSAGE_QUEUE_RK,"测试");
            return new Result(200,"保存成功");
        }else {
            return new Result(500,"保存失败");
        }

    }
    @PostMapping("delete")
    public Result delete(Integer id){
        System.out.println(id);
        int count = postService.delById(id);
        if (count!=0){
            return new Result(200,"删除成功");
        }else {
            return new Result(500,"删除失败");
        }
    }
    @PostMapping("del")
    public Result del(Integer id) {
        System.out.println(id);
        for (int i = 0; i > 1; i++) {
            int count = postService.delById(id);

            if (count != 0) {
                return new Result(200, "删除成功");
            } else {
                return new Result(500, "删除失败");
            }
        }
        return null;
    }

    @PostMapping("update")
    public Result update(Post post){
        System.out.println(post);
        int count = postService.update(post);
        if (count!=0){
            return new Result(200,"更新成功");
        }else{
            return new Result(500,"更新失败");
        }
    }

    @GetMapping("searchAll")
    public Result searchAll(){
//        PageInfo<SearchDTO> pageInfo = searchService.searchAll(pageNum, pageSize);
//        List<SearchDTO> list = pageInfo.getList();
        List<SearchDTO> list = searchService.searchAll();
        if (list==null){
            return new Result(500,"当前没有数据");
        }else {
            return new Result(200,"查询成功",list);
        }
    }

    @PostMapping("searchByWord")
    public Result searchByWord(SearchWordDTO searchWordDTO){
        System.out.println("================================");
        System.out.println(searchWordDTO);
        List<SearchDTO> list = searchService.searchByWord(searchWordDTO);
        list.forEach(System.out::println);
        if (list==null){
            return new Result(500,"没有对应条件的求职者");
        }else {
            return new Result(200,"查询到相关内容的求职者",list);
        }
    }

}
